﻿using FirstFloor.ModernUI.Windows.Controls;
using FirstFloor.ModernUI.Windows.Navigation;
using LinnerToolkit.Desktop.ModernUI.Windows.Controls;
using SimulationPTSystem.Evaluation.Enums;
using SimulationPTSystem.Evaluation.ViewModels;
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SimulationPTSystem.Evaluation.Views
{
    /// <summary>
    /// TrainingPage.xaml 的交互逻辑
    /// </summary>
    public partial class TrainingPage : NavigationPage
    {
        public TrainingPageViewModel TrainingPageViewModel { get; }

        int actualWidth = 0, actualHeight = 0;
        public TrainingPage()
        {
            InitializeComponent();

            TrainingPageViewModel = DataContext as TrainingPageViewModel;
            TrainingPageViewModel.TrainerCollection.CollectionChanged += TrainerCollection_CollectionChanged;

            actualWidth = Screen.PrimaryScreen.Bounds.Width;
            actualHeight = Screen.PrimaryScreen.Bounds.Height;

            // 设置窗体及内部控件尺寸
            borderBindTrainer.Width = gridTraining.Width = root.Width = this.Width = SystemParameters.PrimaryScreenWidth;
            borderBindTrainer.Height = gridTraining.Height = root.Height = this.Height = SystemParameters.PrimaryScreenHeight;

            // 设置监控面板控件样式
            trainerCtrls.Style = FindResource("TrainerCtrlsBigStyle") as Style;

            Loaded += TrainingPage_Loaded;
        }

        private void TrainerCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            WrapPanel panel = (trainerCtrls.Template.FindName("wrapPanel", trainerCtrls) as WrapPanel);
            if (trainerCtrls.Items.Count <= 12)
            {
                panel.ItemWidth = actualWidth / 4;
                panel.ItemHeight = (actualHeight * 0.9) / 3;
                trainerCtrls.FontSize = 12 * panel.ItemWidth / 400;
            }
            else if (trainerCtrls.Items.Count == 13)
            {
                panel.ItemWidth = (actualWidth * 0.95) / 6;
                panel.ItemHeight = actualHeight / 4;
                trainerCtrls.FontSize = 12 * panel.ItemWidth / 400;
            }
        }

        private void TrainingPage_Loaded(object sender, RoutedEventArgs e)
        {
            WrapPanel panel = (trainerCtrls.Template.FindName("wrapPanel", trainerCtrls) as WrapPanel);
            panel.ItemWidth = actualWidth / 4;
            panel.ItemHeight = (actualHeight * 0.9) / 3;
            trainerCtrls.FontSize = 12 * panel.ItemWidth / 400;

            TrainingPageViewModel.AddAllChengYuanCommand.Execute(null);
        }

        private void btnShowBindBorder_Click(object sender, RoutedEventArgs e)
        {
            borderBindTrainer.Visibility = Visibility.Visible;
        }

        private void AnimationButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is AnimationButton btn)
            {
                if (TrainingPageViewModel.ProcessController.IsStarted && ModernDialog.ShowMessage("是否要结束训练？", "提示", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                    return;

                btn.Content = TrainingPageViewModel.ProcessController.IsStarted ? "开始训练" : "结束训练";
                TrainingPageViewModel.StartCommand.Execute(null);
            }
        }

        private void btnHideBorder_Click(object sender, RoutedEventArgs e)
        {
            borderBindTrainer.Visibility = Visibility.Hidden;
        }

        public override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            borderBindTrainer.Visibility = Visibility.Hidden;
        }
    }

    public class RaiseHandToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool raiseHand = (bool)value;
            return new BitmapImage(new Uri(raiseHand ? @"/Assets/Image/Training/RaiseHand_1.png" :
                @"/Assets/Image/Training/RaiseHand_2.png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class C_valueToImage : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte c_value = (byte)((float)value);
            int index = (int)(c_value / 12.5) + 1;
            if (index > 8)
                index = 8;
            return (ImageSource)new BitmapImage(new Uri(@"/Assets/Image/Training/Human_" + index.ToString() + ".png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class H_valueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format("H : {0}", ((float)value).ToString("0"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class B_valueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format("B : {0}", ((float)value).ToString("0"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    #region 12/24屏监控界面

    public class TrainingStateToVisibility : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TrainingState trainingState = (TrainingState)value;
            if (trainingState == TrainingState.Started)
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    public class WIFIToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool wifi = (bool)value;
            return new BitmapImage
                (new Uri(wifi ? @"/Assets/Image/Training/WIFI_1.png" : @"/Assets/Image/Training/WIFI_2.png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class TeamFlagToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TeamFlag teamFlag = (TeamFlag)value;
            BitmapImage bi;
            switch (teamFlag)
            {
                case TeamFlag.Red:
                default:
                    bi = new BitmapImage(new Uri(@"/Assets/Image/Training/TeamFlag_Red.png", UriKind.RelativeOrAbsolute));
                    break;
                case TeamFlag.Yellow:
                    bi = new BitmapImage(new Uri(@"/Assets/Image/Training/TeamFlag_Yellow.png", UriKind.RelativeOrAbsolute));
                    break;
                case TeamFlag.Blue:
                    bi = new BitmapImage(new Uri(@"/Assets/Image/Training/TeamFlag_Blue.png", UriKind.RelativeOrAbsolute));
                    break;
                case TeamFlag.Green:
                    bi = new BitmapImage(new Uri(@"/Assets/Image/Training/TeamFlag_Green.png", UriKind.RelativeOrAbsolute));
                    break;
            }
            return bi;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    public class Pulse_Basic_ValueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format("脉:{0}次/分", ((float)value).ToString("0.00"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class Respiratory_Basic_ValueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format("呼:{0}次/分", ((float)value).ToString("0"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class IERate_Basic_ValueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format("吸呼比 : {0}", ((float)value).ToString("0.00"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class PulseSignalToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool pulseSignal = (bool)value;
            return (ImageSource)new BitmapImage(new Uri(pulseSignal ? @"/Assets/Image/Training/PulseSignal_1.png" :
                @"/Assets/Image/Training/PulseSignal_2.png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class PulseErrorStateToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ErrorState pulseErrorState = (ErrorState)value;
            string returnStr = "";
            switch (pulseErrorState)
            {
                case ErrorState.Interrupt:
                    returnStr = "脉搏信号异常";
                    break;
                case ErrorState.Disturb:
                    returnStr = "异常活动，脉搏信号受干扰";
                    break;
                default:
                    returnStr = "";
                    break;
            }
            return returnStr;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class PulseErrorStateToColor : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ErrorState pulseErrorState = (ErrorState)value;
            switch (pulseErrorState)
            {
                case ErrorState.Interrupt:
                    return Brushes.Yellow;
                case ErrorState.Disturb:
                default:
                    return Brushes.Red;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BreatheSignalToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool breatheSignal = (bool)value;
            return new BitmapImage(new Uri(breatheSignal ? @"/Assets/Image/Training/BreatheSignal_1.png" :
                @"/Assets/Image/Training/BreatheSignal_2.png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BreatheErrorStateToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ErrorState pulseErrorState = (ErrorState)value;
            switch (pulseErrorState)
            {
                case ErrorState.Interrupt:
                    return "呼吸信号异常";
                case ErrorState.Disturb:
                    return "异常活动，呼吸信号受干扰";
                default:
                    return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BreatheErrorStateToColor : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ErrorState pulseErrorState = (ErrorState)value;
            switch (pulseErrorState)
            {
                case ErrorState.Interrupt:
                    return Brushes.Yellow;
                case ErrorState.Disturb:
                default:
                    return Brushes.Red;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class DisturbTimeToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TimeSpan disturbTime = (TimeSpan)value;
            return string.Format("{0}分{1}秒", disturbTime.Minutes.ToString(), disturbTime.Seconds.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class PulseToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int pulse = (int)((float)value);
            return string.Format("{0}次/分", pulse.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class RespiratoryRateToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float respiratoryRate = (float)value;
            return string.Format("{0}次/分", respiratoryRate.ToString("0"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class HPN_valueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float hpn_value = (float)value;
            return string.Format("H: {0}", hpn_value.ToString("0.00"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BPN_valueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float bpn_value = (float)value;
            return string.Format("B: {0}", bpn_value.ToString("0.00"));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class IsAPlayerWorkToColor : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isAPlayerWork = (bool)value;
            return isAPlayerWork ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class IsBPlayerWorkToColor : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isBPlayerWork = (bool)value;
            return isBPlayerWork ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class OrderInTeamToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int orderInTeam = (int)value;
            if (orderInTeam == 0)
            {
                return "";
            }
            else
            {
                return string.Format("排名:{0}", orderInTeam.ToString());
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class MachineSignalToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool machineSignal = (bool)value;
            return new BitmapImage(new Uri(machineSignal ? @"/Assets/Image/Training/MachineSignal_1.png" :
                @"/Assets/Image/Training/MachineSignal_2.png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class MachineSignalToVisibility : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool machineSignal = (bool)value;
            return machineSignal ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    public class ChangeMusicToImageSource : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool changeMusic = (bool)value;
            return new BitmapImage(new Uri(changeMusic ? @"/Assets/Image/Training/ChangeMusic_1.png" :
                @"/Assets/Image/Training/ChangeMusic_2.png", UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class MachineIDToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return $"NO.{0}";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class C_valueToScaleX : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float c_value = (float)value;
            return (double)(c_value / 100);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class C_valueToString : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float c_value = (float)value;
            return c_value.ToString("0") + "%";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    #endregion

    public class IsStartedToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? "结束训练" : "开始训练";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class IsStartedToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
